package com.mall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.shop.entity.product.SkuImages;
import com.mall.shop.entity.product.SkuInfo;
import com.mall.shop.entity.product.SpuInfo;
import com.mall.shop.entity.product.SpuInfoDesc;
import com.mall.shop.mapper.SkuInfoMapper;
import com.mall.shop.service.*;
import com.mall.shop.vo.product.SkuItemSaleAttrVO;
import com.mall.shop.vo.product.SkuItemVO;
import com.mall.shop.vo.product.SpuItemAttrGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.*;

/**
 * sku信息(SkuInfo)表服务实现类
 *
 * @author 龙茶清欢
 * @since 2023-01-16 15:32:39
 */
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private AttrGroupService attrGroupService;


    @Override
    public SkuItemVO getSkuItem(Long skuId) {
        SkuItemVO skuItemVO = new SkuItemVO();
        // 1、获取SKU基本信息，查 pms_sku_info
        SkuInfo skuInfo = this.baseMapper.selectById(skuId);
        skuItemVO.setSkuInfo(skuInfo);

        // 2、获取SKU图片信息，查 pms_sku_images
        SkuImages skuImages = skuImagesService.getById(skuId);
        LambdaQueryWrapper<SkuImages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkuImages::getSkuId, skuId);
        List<SkuImages> skuImagesList = skuImagesService.list(queryWrapper);
        skuItemVO.setImages(skuImagesList);

        // 3、获取SPU的销售属性组合
        List<SkuItemSaleAttrVO> saleAttrVOS = skuSaleAttrValueService.getSalesAttrValueBySpuId(skuInfo.getSpuId());
        skuItemVO.setSaleAttrList(saleAttrVOS);

        // 4、获取SPU的介绍
        SpuInfo spuInfo = spuInfoService.getById(skuInfo.getSpuId());
        SpuInfoDesc spuInfoDesc = spuInfoDescService.getById(skuInfo.getSpuId());
        skuItemVO.setSpuInfoDesc(spuInfoDesc);

        // 5、获取SPU的规格参数信息
        List<SpuItemAttrGroupVO> spuItemAttrGroupVOS = attrGroupService.getAttrGroupAndAttrsBySpuId(skuInfo.getSpuId());
        skuItemVO.setGroupAttrVOS(spuItemAttrGroupVOS);

        return skuItemVO;
    }

    @Autowired
    private ThreadPoolExecutor executor;

    public SkuItemVO getSkuItemDemo(Long skuId) throws ExecutionException, InterruptedException, TimeoutException {
        SkuItemVO skuItemVO = new SkuItemVO();
        // 1、获取SKU基本信息，查 pms_sku_info
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = this.baseMapper.selectById(skuId);
            skuItemVO.setSkuInfo(skuInfo);
            return skuInfo;
        }, executor);

        // 2、获取SKU图片信息，查 pms_sku_images
        CompletableFuture<Void> skuImagesFuture = CompletableFuture.runAsync(() -> {
            LambdaQueryWrapper<SkuImages> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SkuImages::getSkuId, skuId);
            List<SkuImages> skuImagesList = skuImagesService.list(queryWrapper);
            skuItemVO.setImages(skuImagesList);
        }, executor);

        // 3、获取SPU的销售属性组合
        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync((response) -> {
            List<SkuItemSaleAttrVO> saleAttrVOS = skuSaleAttrValueService.getSalesAttrValueBySpuId(response.getSpuId());
            skuItemVO.setSaleAttrList(saleAttrVOS);
        }, executor);

        // 4、获取SPU的介绍
        CompletableFuture<Void> spuInfoDescFuture = skuInfoFuture.thenAcceptAsync((response) -> {
            SpuInfoDesc spuInfoDesc = spuInfoDescService.getById(response.getSpuId());
            skuItemVO.setSpuInfoDesc(spuInfoDesc);
        }, executor);

        // 5、获取SPU的规格参数信息
        CompletableFuture<Void> spuItemAttrGroupFuture = skuInfoFuture.thenAcceptAsync((response) -> {
            List<SpuItemAttrGroupVO> spuItemAttrGroupVOS = attrGroupService.getAttrGroupAndAttrsBySpuId(response.getSpuId());
            skuItemVO.setGroupAttrVOS(spuItemAttrGroupVOS);
        }, executor);

        // CompletableFuture.allOf 将所有的异步编排任务组合起来，感知整个异步任务执行过程，一旦所有异步任务都结束，该CompletableFuture等待结束，如果超过10秒还没执行完直接抛出异常
        CompletableFuture.allOf(skuInfoFuture,skuImagesFuture,saleAttrFuture,spuInfoDescFuture,spuItemAttrGroupFuture).get(10, TimeUnit.SECONDS);
        return skuItemVO;
    }
    // response 就是 skuInfoFuture 中封装的 SkuInfo 返回实例
}

